package com.leyou.item.service;


import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    public List<CategoryDTO> queryByIdParent(Long id) {
        //查询条件，就是父类目的id
        Category record = new Category();
        record.setParentId(id);
        //查询
        List<Category> list = categoryMapper.select(record);

        if (CollectionUtils.isEmpty(list)) {
            //没有查到，返回404
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        //把Category对象，转为DTO对象
        return BeanHelper.copyWithCollection(list, CategoryDTO.class);
    }

    public List<CategoryDTO> queryByIds(List<Long> ids) {
        List<Category> list = categoryMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(ids)){
            throw new LyException(ExceptionEnum.CARTS_NOT_FOUND);
        }
        //把Category对象，转为DTO对象
        return BeanHelper.copyWithCollection(list,CategoryDTO.class);
    }
}
